package bud.mobile.controller;

import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import bud.mobile.service.IMobileMemberService;
import bud.vo.LocationDTO;
import bud.vo.MemberDTO;

@Controller
@Slf4j
public class SettingsController {
	@Inject
	private IMobileMemberService service;
	public IMobileMemberService getService() {
		return service;
	}
	public void setService(IMobileMemberService service) {
		this.service = service;
	}
	
	@RequestMapping("settings/setMain")
	public String setMain(MemberDTO member, HttpSession session){
		if (member.getMem_sec_tofriend() != null){
			service.updateMember(member);
			MemberDTO memberNew = (MemberDTO)session.getAttribute("memberSession");
			memberNew.setMem_sec_topeople(member.getMem_sec_topeople());
			memberNew.setMem_sec_tofriend(member.getMem_sec_tofriend());
			session.setAttribute("memberSession", memberNew);
			
			Map<Integer, LocationDTO> userLocation = (Map<Integer, LocationDTO>) session.getServletContext().getAttribute("userLocation");
			LocationDTO location = userLocation.get(memberNew.getMem_no());
			if (location != null){
				location.setLo_sec_tofriend(member.getMem_sec_tofriend());
				location.setLo_sec_topeople(member.getMem_sec_topeople());
				userLocation.put(location.getLo_ref_mem(), location);
			}
		}
		return "settings/setMain";
	}
	@RequestMapping("settings/setGuide")
	public String setGuide(){
		return "settings/setGuide";
	}
	@RequestMapping("settings/setSecurity")
	public String setSecurity(){
		return "settings/setSecurity";
	}
	@RequestMapping("settings/setAccount")
	public String setAccount(){
		return "settings/setAccount";
	}
	
	@RequestMapping("settings/setPass")
	public String setPass(Model model, String edit_pass, String edit_pass_new, String edit_pass_confirm, HttpSession session){
		MemberDTO member = (MemberDTO)session.getAttribute("memberSession");
		if (edit_pass == null && edit_pass_new == null && edit_pass_confirm == null){
			return "settings/setAccount";
		}
		if (member.getMem_pass().equals(edit_pass)){
			if (!edit_pass_new.equals(edit_pass_confirm)){
				model.addAttribute("msg", "새 비밀번호가 일치하지 않습니다");
				return "settings/setAccount";
			}
			MemberDTO memberNew = new MemberDTO();
			memberNew.setMem_no(member.getMem_no());
			memberNew.setMem_pass(edit_pass_new);
			service.updateMember(memberNew);
			memberNew = service.getMember(member.getMem_no());
			session.setAttribute("memberSession", memberNew);
		} else {
			model.addAttribute("msg", "현재 비밀번호가 다릅니다");
			return "settings/setAccount";
		}
		model.addAttribute("msg", "비밀번호 변경 완료!");
		return "settings/setMain";
	}
	
	@RequestMapping("settings/deleteMember")
	public String deleteMember(MemberDTO member, String pass, HttpSession session, Model model){
		MemberDTO memberSession = (MemberDTO)session.getAttribute("memberSession");
		if (!memberSession.getMem_email().equals(member.getMem_email())){
			model.addAttribute("msg", "올바른 이메일 주소를 입력하세요");
			return "settings/setAccount";
		} else if (!member.getMem_pass().equals(pass) || !memberSession.getMem_pass().equals(pass)){
			model.addAttribute("msg", "비밀번호가 일치하지 않습니다");
			return "settings/setAccount";
		}
		service.deleteMember(member);
		model.addAttribute("msg", "회원탈퇴가 정상적으로 완료되었습니다");
		session.invalidate();
		return "login";
	}
	
}
